# from .client_get_or_input_data import get_or_input_data
from datetime import datetime

# from torchgen.model import Return

from index.models import *
from index.bs_control import *

#处理秒级数据
def bs_second_data_format_transformation(bs_id):
        # Step 1: 从不同来源获取字段数据
        # if bs_id == 1:
        # cell_1_user_count = action("read_user_count_cell_1")
        # cell_2_user_count = action("read_user_count_cell_2")
        # uplink_throughput = action("read_speed_uplink")
        # downlink_throughput = action("read_speed_downlink")
        # else:
        # cell_1_user_count = BS_element("user_count_cell_1")
        # cell_2_user_count = BS_element("user_count_cell_2")
        # uplink_throughput = BS_element("uplink")
        # downlink_throughput = BS_element("downlink")
        # 获得用户数数据
    user_count = action("Read_Home")
    cell_1_user_count, cell_2_user_count = user_count
    # 获得吞吐量数据
    getspeed_on()
    speed = read_speed()
    downlink_throughput = speed[0]
    uplink_throughput = speed[2]
    getspeed_on()
    # Step 2: 数据有效性校验
    data_dict = {
        "bs_id": bs_id,
        "cell_1_user_count": cell_1_user_count,
        "cell_2_user_count": cell_2_user_count,
        "uplink_throughput": uplink_throughput,
        "downlink_throughput": downlink_throughput
    }
    BsSecondDataElement.validate_data(data_dict)
    # Step 4: 创建 Bs_second_data 对象
    current_time = datetime.now()  # 当前时间戳
    bs_data = BsSecondDataElement(
        time=current_time,
        bs_id=bs_id,
        cell_1_user_count=cell_1_user_count,
        cell_2_user_count=cell_2_user_count,
        uplink_throughput=uplink_throughput,
        downlink_throughput=downlink_throughput
    )
    return bs_data

#处理分钟级数据
def bs_minute_data_format_transformation(bs_id):
    transmit_power = action("get_MaxTxPower_info")
    transmit_power = transmit_power[0]
    # Step 2: 数据有效性校验
    data_dict = {
        "bs_id": bs_id,
        "transmit_power": transmit_power
    }
    BsMinuteDataElement.validate_data(data_dict)
    # Step 4: 创建 Bs_second_data 对象
    current_time = datetime.now()  # 当前时间戳
    bs_data = BsMinuteDataElement(
        time=current_time,
        bs_id=bs_id,
        transmit_power=transmit_power
    )
    return bs_data

def bs_hour_data_format_transformation(bs_id):
    # Step 1: 获取上下行带宽
    bandwidth_info = action("get_Bandwidth_info")  # 调用接口获取数据
    uplink_bandwidth = bandwidth_info[0]  # 上行带宽
    downlink_bandwidth = bandwidth_info[1]  # 下行带宽

    # Step 2: 获取休眠信息
    energy_save_info = action("get_EnergySave_info")  # 调用接口获取休眠数据
    sleep_period = energy_save_info[0]
    rf_status = energy_save_info[1]
    sleep_status = energy_save_info[2]

    # Step 3: 获取RU状态信息
    ru_info = action("get_RU_info")  # 调用接口获取RU状态信息
    ru_power_status = True if ru_info[0] else False
    rf_status = True if ru_info[1] else False
    ru_status = True if ru_info[2] else False

    # Step 4: 数据校验（可选，根据业务逻辑设置约束）
    if uplink_bandwidth < 0 or downlink_bandwidth < 0:
        raise ValueError("带宽数据无效：不能为负数！")

    data_dict = {
        "bs_id": bs_id,
        "uplink_bandwidth": uplink_bandwidth,
        "downlink_bandwidth": downlink_bandwidth,
        "sleep_status": sleep_status,
        "sleep_period": sleep_period,
        "rf_status": rf_status,
        "ru_power_status": ru_power_status,
        "ru_status": ru_status
    }
    BsHourlyDataElement.validate_data(data_dict)
    current_time = datetime.now()
    bs_data = BsHourlyDataElement(
        time=current_time,
        bs_id=bs_id,
        uplink_bandwidth=uplink_bandwidth,
        downlink_bandwidth=downlink_bandwidth,
        sleep_status=sleep_status,
        sleep_period=sleep_period,
        rf_status=rf_status,
        ru_power_status=ru_power_status,
        ru_status=ru_status
    )
    return bs_data



def inverter_second_data_format_transformation(bs_id):
    inverter_second_data=get_or_input_data("get_data")
    inverter_active_power=inverter_second_data[14]
    inverter_output_current=inverter_second_data[13]
    inverter_output_voltage=inverter_second_data[12]
    inverter_output_freq=inverter_second_data[15]
    pv_active_power=inverter_second_data[11]
    pv_output_current=inverter_second_data[10]
    pv_output_voltage=inverter_second_data[9]
    storage_active_power=inverter_second_data[25]
    storage_output_current=inverter_second_data[17]
    storage_output_voltage=inverter_second_data[16]
    storage_soc=inverter_second_data[19]

    data_dict = {
        #"bs_id": bs_id,
        "inverter_active_power": inverter_active_power,
        "inverter_output_current": inverter_output_current,
        "inverter_output_voltage": inverter_output_voltage,
        "inverter_output_freq": inverter_output_freq,
        "pv_active_power": pv_active_power,
        "pv_output_current": pv_output_current,
        "pv_output_voltage": pv_output_voltage,
        "storage_active_power": storage_active_power,
        "storage_output_current": storage_output_current,
        "storage_output_voltage": storage_output_voltage,
        "storage_soc": storage_soc
    }
    InverterSecondDataElement.validate_data(data_dict)
    current_time = datetime.now()
    bs_data = InverterSecondDataElement(
        #id=bs_id,
        save_time=current_time,
        inverter_active_power=inverter_active_power,
        inverter_output_current=inverter_output_current,
        inverter_output_voltage=inverter_output_voltage,
        inverter_output_freq=inverter_output_freq,
        pv_active_power=pv_active_power,
        pv_output_current=pv_output_current,
        pv_output_voltage=pv_output_voltage,
        storage_active_power=storage_active_power,
        storage_output_current=storage_output_current,
        storage_output_voltage=storage_output_voltage,
        storage_soc=storage_soc,
        information="111"
    )
    return bs_data


def inverter_hourly_data_format_transformation(bs_id):

    inverter_hourly_data=get_or_input_data()
    pv_cumulative_energy=inverter_hourly_data[0]*0
    data_dict = {
        #"bs_id":bs_id,
        "pv_cumulative_energy": pv_cumulative_energy
    }
    InverterHourlyDataElement.validate_data(data_dict)
    current_time = datetime.now()
    bs_data = InverterHourlyDataElement(
        #id=bs_id,
        save_time=current_time,
        pv_cumulative_energy=pv_cumulative_energy,
        information="111"
    )
    return bs_data
